package com.ebt.m.repository.provider;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore.Audio;
import android.provider.MediaStore.Files;
import android.provider.MediaStore.Files.FileColumns;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Video;

import com.ebt.m.R;
import com.ebt.m.repository.provider.FileSortHelper.SortMethod;
import com.ebt.m.utils.ArrayUtil;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

/**
 * 
 * ClassName: FileTypeHelper <br/>
 * Function: 文件分类 <br/>
 * 
 * @author damon.han <br/>
 * @date 2015年5月21日 上午9:44:39 <br/>
 */
public class FileTypeHelper {
	private static final String LOG_TAG = "FileTypeHelper";

	public static final int COLUMN_ID = 0;

	public static final int COLUMN_PATH = 1;

	public static final int COLUMN_SIZE = 2;

	public static final int COLUMN_DATE = 3;

	private static Context mContext;

	public FileTypeHelper(Context context) {
		mContext = context;
	}

	/* 所有文件类型 */
	public enum FileTypeAll {
		FILE_TYPE_FOLDER(0), FILE_TYPE_IMAGE(100), FILE_TYPE_AUDIO(200), FILE_TYPE_VIDEO(300), FILE_TYPE_DOC(400), FILE_TYPE_PPT(401), FILE_TYPE_WORD(402), FILE_TYPE_PDF(403), FILE_TYPE_EXCEL(404), FILE_TYPE_TEXT(410), FILE_TYPE_APK(500), FILE_TYPE_APP(600), FILE_TYPE_WEB(700), FILE_TYPE_SHORTCUT(800), FILE_TYPE_OTHER(1000);
		private final int mValue;

		FileTypeAll(int value) {
			mValue = value;
		}

		public int getValue() {
			return mValue;
		}

		public static FileTypeAll valueOf(int value) {
			for (FileTypeAll c : FileTypeAll.values()) {
				if (value == c.getValue()) {
					return values()[c.ordinal()];
				}
			}
			return null;
		}
	}
	/**
	 *
	 * @Description: 文档类型计划书一
	* @return
	* @return String
	* @throw
	 */
	private String buildDocSelection() {
		StringBuilder selection = new StringBuilder();
		Iterator<String> iter = Util.sDocMimeTypesSet.iterator();
		while (iter.hasNext()) {
			selection.append("(" + FileColumns.MIME_TYPE + "='" + iter.next() + "') OR ");
		}
		return selection.substring(0, selection.lastIndexOf(")") + 1);
	}
	private String buildDocSelectionBack() {
		StringBuilder selection = new StringBuilder();
		// Iterator<String> iter = Util.sDocMimeTypesSet.iterator();
		// while (iter.hasNext()) {
		// selection.append("(" + FileColumns.DATA + "like'" + iter.next() +
		// "') OR ");
		// }

		String[] pptExts = FileIconHelper.getFileExtTypes().get(FileTypeAll.FILE_TYPE_PPT);
		String[] pdfExts = FileIconHelper.getFileExtTypes().get(FileTypeAll.FILE_TYPE_PDF);
		String[] excelExts = FileIconHelper.getFileExtTypes().get(FileTypeAll.FILE_TYPE_EXCEL);
		String[] txtExts = FileIconHelper.getFileExtTypes().get(FileTypeAll.FILE_TYPE_TEXT);
		String[] webExts = FileIconHelper.getFileExtTypes().get(FileTypeAll.FILE_TYPE_WEB);
		String[] wordExts = FileIconHelper.getFileExtTypes().get(FileTypeAll.FILE_TYPE_WORD);
		String[] exts = ArrayUtil.concatAll(pptExts, pdfExts, excelExts, txtExts, webExts, wordExts);
		for (int i = 0; i < exts.length; i++) {
			selection.append("(" + FileColumns.DATA + " like'%." + exts[i] + "') OR ");
		}
		return selection.substring(0, selection.lastIndexOf(")") + 1);
	}

	private String buildSelectionByType(FileTypeAll cat) {
		String selection = null;
		switch (cat) {
			case FILE_TYPE_DOC :
				// selection = buildDocSelection();
				selection = buildDocSelectionBack();
				break;
			default :
				selection = null;
		}
		return selection;
	}

	private Uri getContentUriByType(FileTypeAll cat) {
		Uri uri;
		String volumeName = "external";
		switch (cat) {
			case FILE_TYPE_DOC :
			case FILE_TYPE_APK :// apk文件
				uri = Files.getContentUri(volumeName);
				break;
			case FILE_TYPE_AUDIO :
				uri = Audio.Media.getContentUri(volumeName);
				break;
			case FILE_TYPE_VIDEO :
				uri = Video.Media.getContentUri(volumeName);
				break;
			case FILE_TYPE_IMAGE :
				uri = Images.Media.getContentUri(volumeName);
				break;
			default :
				uri = null;
		}
		return uri;

	}

	private String buildSortOrder(SortMethod sort) {
		String sortOrder = null;
		switch (sort) {
			case name :
				sortOrder = FileColumns.TITLE + " asc";
				break;
			case size :
				sortOrder = FileColumns.SIZE + " asc";
				break;
			case date :
				sortOrder = FileColumns.DATE_MODIFIED + " desc";
				break;
			case type :
				sortOrder = FileColumns.MIME_TYPE + " asc, " + FileColumns.TITLE + " asc";
				break;
		}
		return sortOrder;
	}

	/**
	 * 根据文件类型和排序查询
	 * 
	 * @param fc
	 * @param sort
	 * @return
	 */
	public synchronized Cursor query(FileTypeAll fc, SortMethod sort) {
		Uri uri = getContentUriByType(fc);
		String selection = buildSelectionByType(fc);
		String sortOrder = buildSortOrder(sort);

		if (uri == null) {
			return null;
		}
		String[] columns = new String[]{FileColumns._ID, FileColumns.DATA, FileColumns.SIZE, FileColumns.DATE_MODIFIED};
		return mContext.getContentResolver().query(uri, columns, selection, null, sortOrder);
	}

	public static HashMap<FileTypeAll, Integer> fileTypeStrings = new HashMap<FileTypeAll, Integer>();
	static {
		fileTypeStrings.put(FileTypeAll.FILE_TYPE_FOLDER, R.string.type_folder);
		fileTypeStrings.put(FileTypeAll.FILE_TYPE_IMAGE, R.string.type_picture);
		fileTypeStrings.put(FileTypeAll.FILE_TYPE_AUDIO, R.string.type_audio);
		fileTypeStrings.put(FileTypeAll.FILE_TYPE_VIDEO, R.string.type_video);
		fileTypeStrings.put(FileTypeAll.FILE_TYPE_DOC, R.string.type_document);
		fileTypeStrings.put(FileTypeAll.FILE_TYPE_PPT, R.string.type_ppt);
		fileTypeStrings.put(FileTypeAll.FILE_TYPE_WORD, R.string.type_word);
		fileTypeStrings.put(FileTypeAll.FILE_TYPE_PDF, R.string.type_pdf);
		fileTypeStrings.put(FileTypeAll.FILE_TYPE_EXCEL, R.string.type_excel);
		fileTypeStrings.put(FileTypeAll.FILE_TYPE_APK, R.string.type_apk);
		fileTypeStrings.put(FileTypeAll.FILE_TYPE_APP, R.string.type_app);
		fileTypeStrings.put(FileTypeAll.FILE_TYPE_WEB, R.string.type_web);
		fileTypeStrings.put(FileTypeAll.FILE_TYPE_SHORTCUT, R.string.type_shortcut);
		fileTypeStrings.put(FileTypeAll.FILE_TYPE_TEXT, R.string.type_txt);
		fileTypeStrings.put(FileTypeAll.FILE_TYPE_OTHER, R.string.type_other);
	}

	/**
	 * 根据类型获取类型字符串
	 * 
	 * @param type
	 * @return
	 */
	public String getFileTypeString(FileTypeAll type) {
		Iterator it = fileTypeStrings.entrySet().iterator();
		String result = "";
		FileTypeAll tmp = null;
		while (it.hasNext()) {
			Entry enttry = (Entry) it.next();
			tmp = (FileTypeAll) enttry.getKey();
			if (tmp == type && enttry.getValue() != null) {
				result = mContext.getString((Integer) enttry.getValue());
				break;
			}
		}
		return result;
	}
}
